package modelo;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import entities.AperturaMesaEntity;
import entities.FacturaEntity;
import entities.SucursalEntity;
import hbt.AbstractHibernateManager;


	public class FacturaDao extends AbstractHibernateManager {
		
		private static FacturaDao instancia = null;
		
		public static FacturaDao getInstancia() {
			if(instancia == null) {
				instancia = new FacturaDao();
			}
			return instancia;
		}
		
		public void save (FacturaEntity fact){
			Session s = null;
			try {
				s=getNewSession();
				s.beginTransaction();
				s.save(fact);
				s.flush();
				s.getTransaction().commit();
				System.out.println("[Factura SAVED ON TABLE Facturas]::OK");
			} catch (HibernateException e) {
				e.printStackTrace();
			} finally{
				closeSession(s);
			}
		}
		
		public void guardar (FacturaEntity fact){
			Session s = null;
			try {
				s=getNewSession();
				s.beginTransaction();
				s.persist(fact);
				s.flush();
				s.getTransaction().commit();
				System.out.println("[Factura PERSISTED ON TABLE Facturas]::OK");
			} catch (HibernateException e) {
				e.printStackTrace();
			} finally{
				closeSession(s);
			}
		}
		
		
		
		public void merge (FacturaEntity fact){
			Session s = null;
			try {
				s=getNewSession();
				s.beginTransaction();
				s.merge(fact);
				s.flush();
				s.getTransaction().commit();
				System.out.println("[Factura MERGED ON TABLE Facturas]::OK");
			} catch (HibernateException e) {
				e.printStackTrace();
			} finally{
				closeSession(s);
			}
		}
		
		public boolean saveOrUpdate(FacturaEntity fact){
			Session s= null;
			try {
				s=getNewSession();
				s.beginTransaction();
				s.flush();
				s.saveOrUpdate(fact);
				s.getTransaction().commit();
				System.out.println("[Factura (SAVED OR UPDATED) ON TABLE facturas]::OK");
				return true;
			} catch (HibernateException e) {
				e.printStackTrace();
				return false;
			} finally {
				closeSession(s);
			}
		}
		
		
		public FacturaEntity obtenerFacturaPorApMesa(Integer id)
		{
			Session s = null;	
			FacturaEntity factura=new FacturaEntity();		
			try {
						s=getNewSession();						
						factura= (FacturaEntity) s.createQuery("FROM FacturaEntity f "
									   + "WHERE f.nroapmesa =:id")
									.setInteger("id",id).
									uniqueResult();						
						
					} catch (HibernateException e) {
						e.printStackTrace();
					} finally{
						closeSession(s);
					}
			
			return factura;
		}
		
		public FacturaEntity obtenerFactura(Integer id)
		{
			Session s = null;	
			FacturaEntity factura=new FacturaEntity();		
			try {
						s=getNewSession();						
						factura= (FacturaEntity) s.createQuery("FROM FacturaEntity f "
									   + "WHERE f.nroFactura =:id")
									.setInteger("id",id).
									uniqueResult();						
						
					} catch (HibernateException e) {
						e.printStackTrace();
					} finally{
						closeSession(s);
					}
			
			return factura;
		}
		
		public Integer obtenerUltimoIdfact(String estado)
		{
			Session s = null;	
			Integer id = 0;
			try {
						s=getNewSession();						
						id= (Integer) s.createQuery("select max(nroFactura) FROM FacturaEntity f "
									   + "WHERE f.estado =:estado").setString("estado", estado).uniqueResult();						
						
					} catch (HibernateException e) {
						e.printStackTrace();
					} finally{
						closeSession(s);
					}
			
			return id;
			
		}
		
}
